import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.swing.JOptionPane;
//import javax.swing.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.net.*;

//
//Assume connection to database that returns a diagnostic
//This checks validity of the data

//might need this package:   http://www.oracle.com/technetwork/java/javamail/index.html
//email example:  http://www.javacommerce.com/displaypage.jsp"name=javamail.sql&id=18274

//dataType variable will probably be a value returned from the database table -I"m just using the
//Strings  Engine, Transmission, etc. because I don"t know what values are actually
//named/stored in Evan"s table

//Needs Integrated/values and variable names may need changed
public class IA{
    static User user;
    static Car car;
	static boolean done = false;

    public IA(User u, Car c, ArrayList<String> alist)
    {
        this.user = u;
        this.car = c;
        checkData(alist);
    }
    public static void main(String [] args)
    {
        ArrayList<String> hmm = new ArrayList <String>();
        hmm.add("12");
        hmm.add("130");
        hmm.add("20");
        hmm.add("35");
        hmm.add("30000");
        hmm.add("90");
        hmm.add("90");
        hmm.add("Good");
        hmm.add("123");
        hmm.add("123");
        hmm.add("123");
        hmm.add("123");
        hmm.add("123");
        hmm.add("132");
        hmm.add("Dumb");
        hmm.add("90");
        hmm.add("Yes");
        hmm.add("Yes");
        hmm.add("Yes");
        hmm.add("75");
        hmm.add("75");
        hmm.add("High");
        hmm.add("Good");
        hmm.add("Yes");
        hmm.add("50");
        hmm.add("Good");
        hmm.add("Good");
        hmm.add("Good");
        hmm.add("Good");
        hmm.add("Good");
        hmm.add("Good");
        hmm.add("Good");
        hmm.add("Good");
        checkData(hmm);
    }
public static void checkData(ArrayList<String> arr)
{
    int index = 1;
    String typeString = null;
    for (String tmp : arr)
    {
		if (done == false) {
			System.out.println("Index: "+index);
			switch (index)
			{
					case 1:  typeString = "RPM";       break;   //done
                    case 2:  typeString = "Engine Temp";      break;//done
                    case 3:  typeString = "Tire Pressure";         break;//done
                    case 4:  typeString = "Fuel Economy";         break;//done
                    case 5:  typeString = "Mileage";           break;//done
                    case 6:  typeString = "Oil Life";          break;//done
                    case 7:  typeString = "Fuel Level";          break;//done
                    case 8:  typeString = "Transmission Status";        break;
                    case 9:  typeString = "Time in Neutral";     break;//done
                    case 10: typeString = "Time in Drive";       break;//done
                    case 11: typeString = "Time in Reverse";      break;//done
                    case 12: typeString = "Time in Gear 1";      break;//done
                    case 13: typeString = "Time in Gear 2"; break;//done
                    case 14: typeString = "Time in Gear 3"; break;//done
                    case 15: typeString = "Infotainment Status";    break;
                    case 16: typeString = "Radio Frequency";    break;
                    case 17: typeString = "Infotainment Connected";     break;
                    case 18: typeString = "CD";     break;
                    case 19: typeString = "iPod";   break;
                    case 20: typeString = "Internal Temp";  break;//done
                    case 21: typeString = "External Temp";  break;//done
                    case 22: typeString = "Fan Speed";  break;
                    case 23: typeString = "Climate Status";     break;
                    case 24: typeString = "Phone Connection";   break;
                    case 25: typeString = "Call Time";  break;//done
                    case 26: typeString = "Communication Status";   break;
                    case 27: typeString = "Headlight";  break;
                    case 28: typeString = "Power Outlet";   break;
                    case 29: typeString = "Internal Lights";    break;
                    case 30: typeString = "Suspension Status";  break;
                    case 31: typeString = "Brake Fluid Status";     break;//done
                    case 32: typeString = "Brake Status";   break;
                    case 33: typeString = "Steering Status";    break;
					default: typeString = "foo"; break;
					
                }

            dataCheck (tmp, typeString);
		}

        index ++;
    }
}
 public static boolean checkIfInt(String in) {

        try {

            Integer.parseInt(in);

        } catch (NumberFormatException ex) {
            return false;
        }

        return true;
    }

	public static boolean checkIfDouble(String in) {
		
        try {
			
            Double.parseDouble(in);
			
        } catch (NumberFormatException ex) {
            return false;
        }
		
        return true;
    }
	
public static void dataCheck(String diag,String subType){

    //User user = AutoBiographyGUI.currUser; //retrieved from DB
    //Car car = AutoBiographyGUI.currCar;   //retrieved from DB

        int safeLowerBound, safeUpperBound;
        double diagnostic =0;
	int diagnosticInt=0;	
        String d = diag;
        if(checkIfInt(d))
        {
            diagnosticInt = Integer.parseInt(d);
        }
		if (checkIfDouble(d)) {
			diagnostic = Double.parseDouble(d);
		}

//Handles possible errors in the engine


        if(subType.equals("Engine Temp")){

                    safeLowerBound=50;// safe low value for running engines,  maybe like 50 F
                    safeUpperBound=300;// safe high value for engine temp-maybe like 300 F"

                    if(diagnosticInt<safeLowerBound || diagnosticInt>safeUpperBound){
                        new openhtml("Pittsburgh, PA");
                        JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
                             sendDataEmail(subType, user, car);
                             // I don"t know if we are suppose to have a handler type thing here
                             // Or if we are just supposed to display that the data is possibly bad
                     }
    }
	
		else if(subType.equals("foo")){
			done = true;
		}
        else if(subType.equals("RPM")){
            safeLowerBound=0;// safe low value for RPM; probly 0
                    safeUpperBound=6000;// safe high value for RPM; probly 5000-6000


                     if(diagnosticInt<safeLowerBound || diagnosticInt>safeUpperBound){

                         new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                             // I don"t know if we are suppose to have a handler type thing here
                             // Or if we are just supposed to display that the data is possibly bad
                     }
    }
        else if(subType.equals("Mileage")){

            safeLowerBound=0;// safe low value for mileage, probly 0
                    safeUpperBound=300000;// safe high value for mileage, probly 300000

             if(diagnosticInt<safeLowerBound || diagnosticInt>safeUpperBound){
                 new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                             // I don"t know if we are suppose to have a handler type thing here
                             // Or if we are just supposed to display that the data is possibly bad
                     }
        }
        else if(subType.equals("Fuel Economy")){

            safeLowerBound=7;// safe low value for economy, probly 5-10mpg
                    safeUpperBound=60;// safe high value for economy, probly 50-60mpg

			System.out.println("F.E.: "+diagnostic);
                 if(diagnostic<safeLowerBound || diagnostic>safeUpperBound){
                     new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                             // I don"t know if we are suppose to have a handler type thing here
                             // Or if we are just supposed to display that the data is possibly bad
                     }
        }
        else if(subType.equals("Oil Life")){

            safeLowerBound=0;// safe low value for oil life, probly 0
                    safeUpperBound=100;// safe high value for oil life, probly 100

                 if(diagnostic<safeLowerBound || diagnostic>safeUpperBound){
                     new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                             // I don"t know if we are suppose to have a handler type thing here
                             // Or if we are just supposed to display that the data is possibly bad
                     }
        }
        else if(subType.equals("Fuel Level")){

            safeLowerBound=0;// safe low value for fuel%, probly 0
                    safeUpperBound=100;// safe high value for fuel%, probly 100

                 if(diagnostic<safeLowerBound || diagnostic>safeUpperBound){
                     new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                             // I don"t know if we are suppose to have a handler type thing here
                             // Or if we are just supposed to display that the data is possibly bad
                     }


    }

else if(subType.equals("Transmission Status")){

                    safeLowerBound=0;// safe low value for fluid,  probly 0
                    safeUpperBound=30;// safe high value for fluid, probly 100

                    if(!d.equals("Good")){
                        new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                             // I don"t know if we are suppose to have a handler type thing here
                             // Or if we are just supposed to display that the data is possibly bad
                     }
    }



        else if(subType.equals("Brake Fluid Status")){

                    safeLowerBound=0;// safe low value for fluid,  probly 0
                    safeUpperBound=30;// safe high value for fluid, probly 100

                    if(diagnostic<safeLowerBound || diagnostic>safeUpperBound){
                        new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                             // I don"t know if we are suppose to have a handler type thing here
                             // Or if we are just supposed to display that the data is possibly bad
                     }
    }






//Handles possible errors with infotainment
        else if(subType.equals("Infotainment Status"))
        {
                if(!d.equals("Good")){
                    new openhtml("Pittsburgh, PA");
                    JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                        // I don"t know if we are suppose to have a handler type thing here
                        // Or if we are just supposed to display that the data is possibly bad
                }
        }//end infotainment

     

//Handles possible errors with climate


        else if(subType.equals("Internal Temp")){

                    safeLowerBound=-20;// safe low value for int temp  probly 50 F
                    safeUpperBound=120;// safe high value for int temp, probly 100 F

                    if(diagnostic<safeLowerBound || diagnostic>safeUpperBound){
                        new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                             // I don"t know if we are suppose to have a handler type thing here
                             // Or if we are just supposed to display that the data is possibly bad
                     }
    }
        else if(subType.equals("External Temp")){

            safeLowerBound=-20;// safe low value for ext temp probly -30 F
                    safeUpperBound=125;// safe high value for ext temp; probly 130 F


                     if(diagnostic<safeLowerBound || diagnostic>safeUpperBound){
                         new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                             // I don"t know if we are suppose to have a handler type thing here
                             // Or if we are just supposed to display that the data is possibly bad
                     }
    }




//Handles possible errors in Communication


       else if(subType.equals("Climate Status"))
        {
			System.out.println("Clim: "+d);
                if(!d.equals("Good")){
                    new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                        // I don"t know if we are suppose to have a handler type thing here
                        // Or if we are just supposed to display that the data is possibly bad
                }
        }
    
       else if(subType.equals("Communication Status"))
        {
                if(!d.equals("Good")){
                    new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                        // I don"t know if we are suppose to have a handler type thing here
                        // Or if we are just supposed to display that the data is possibly bad
                }
        }//end infotainment
else if(subType.equals("Headlight"))
        {
                if(!d.equals("Good")){
                    new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                        // I don"t know if we are suppose to have a handler type thing here
                        // Or if we are just supposed to display that the data is possibly bad
                }
        }
else if(subType.equals("Power Outlet"))
        {
                if(!d.equals("Good")){
                    new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                        // I don"t know if we are suppose to have a handler type thing here
                        // Or if we are just supposed to display that the data is possibly bad
                }
        }

//Handles Lighting/Electrical problems
        else if(subType.equals("Internal Lights")){

               if(!d.equals("Good")){
                   new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                        // I don"t know if we are suppose to have a handler type thing here
                        // Or if we are just supposed to display that the data is possibly bad
                }
    }//end electircal/lighting

 else if(subType.equals("Suspension Status")){

               if(!d.equals("Good")){
                   new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
   sendDataEmail(subType, user, car);
                        // I don"t know if we are suppose to have a handler type thing here
                        // Or if we are just supposed to display that the data is possibly bad
                }
    }
 else if(subType.equals("Brake Status")){

               if(!d.equals("Good")){
                   new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
   sendDataEmail(subType, user, car);
                        // I don"t know if we are suppose to have a handler type thing here
                        // Or if we are just supposed to display that the data is possibly bad
                }
    }
 else if(subType.equals("Steering Status")){

	 System.out.println("Steering: "+d);
               if(!d.equals("Good")){
                   new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
 sendDataEmail(subType, user, car);
                        // I don"t know if we are suppose to have a handler type thing here
                        // Or if we are just supposed to display that the data is possibly bad
                }
    }//end electircal/lighting
//Handles Suspension, Brakes, Tires, and Steering possible errors

        else if(subType.equals("Tire Pressure")){

                    safeLowerBound=10;// safe low value for tire pressure, probly 10 psi
                    safeUpperBound=50;// safe high value for tire pressure, probly 50 psi

                    if(diagnostic<safeLowerBound || diagnostic>safeUpperBound){
                        new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
sendDataEmail(subType, user, car);
                             // I don"t know if we are suppose to have a handler type thing here
                             // Or if we are just supposed to display that the data is possibly bad
                     }
    }
        else if(subType.equals("Brake Status")){

            //safeLowerBound=0;// safe low value for brake fluid time for change, probly 0
                    //safeUpperBound=3;// safe high value for brake fluid time change, probly 3 years


                     //if(diagnostic<safeLowerBound || diagnostic>safeUpperBound){
                        if(!d.equals("Good")){
                                new openhtml("Pittsburgh, PA");
JOptionPane.showMessageDialog(null,"Data may be corrupt! Notifications have been sent!","Alert:" + subType,JOptionPane.ERROR_MESSAGE);
sendDataEmail(subType, user, car);
                             // I don"t know if we are suppose to have a handler type thing here
                             // Or if we are just supposed to display that the data is possibly bad
                     }
        }
        //end Brakes,tires, etc.
}//end dataCheck

public static void sendDataEmail(String type, User u, Car c){
    System.out.println("creating email to send");
    String body = "testing testing testing";
   // String body = "Unsafe Daignostics found in" + u.username + "'s" + c.model + " pertaining to " + type;
        //sendEmail("coe-1186-project-team@googlegroups.com",, "Unsafe Diagnostics", body);
        sendEmail("emm82@pitt.edu", "Unsafe Diagnostics", body);
    //body = "Unsafe Diagnostic pertaining to " + type + " in your " + c.model+ ".  Problem has been reported. You will be contacted with further information.";
    //sendEmail("coe-1186-project-team@google.groups.com", u.email, "Unsafe Diagnostics Found and reported", body);
}//end sendDataEmail




//private static Properties fMailServerConfig = new Properties();


				   public static void sendEmail(String aToEmailAddr, String aSubject, String aBody ){
					
					// Recipient's email ID needs to be mentioned.
					//String to = "emm82@pitt.edu";
					String to = aToEmailAddr;
					// Sender's email ID needs to be mentioned
					String from = "coe-1186-project-team@googlegroups.com";
					
					// Assuming you are sending email from localhost
					String host = "smtp.gmail.com";
					
					// Get system properties
					Properties props = System.getProperties();
					props.put("mail.transport.protocol", "smtp");
					props.setProperty("mail.smtp.host", host);
					props.setProperty("mail.smtp.user", from);
					props.setProperty("mail.smtp.password", "COE1186");
					props.setProperty("mail.smtp.port", "587"); // 587 is the port number of yahoo mail
					props.setProperty("mail.smtp.auth", "true");
					props.put("mail.smtp.starttls.enable","true");
					
					
					
					// Get the default Session object.
					Session session = Session.getDefaultInstance(props, new MyAuthenticator());
					
					try{
						// Create a default MimeMessage object.
						MimeMessage message = new MimeMessage(session);
						
						// Set From: header field of the header.
						message.setFrom(new InternetAddress(from));
						
						// Set To: header field of the header.
						message.addRecipient(Message.RecipientType.TO,
											 new InternetAddress(to));
						
						// Set Subject: header field
						message.setSubject(aSubject);
						
						// Now set the actual message
						message.setText(aBody);
						
						System.out.println("About to send message.");
						// Send message
						URLName urln = new URLName("smtp", host, 587, "", "coe-1186-project-team" , "COE1186");
						
						com.sun.mail.smtp.SMTPSSLTransport trans = new com.sun.mail.smtp.SMTPSSLTransport(session, urln);
						
						trans.setStartTLS(true);
						//trans.send(message);
						trans.close();
						System.out.println("Sent message successfully....");
					}catch (MessagingException mex) {
						mex.printStackTrace();
					}
  }
}





